import ISdkManager from "./ISdkManager";
import WxSdkManager from "./WxSdkManager";
import ByteSdkManager from "./ByteSdkManager";
import ResLoadHelper from "./ResLoadHelper";
import SdkUIBase from "./SdkUIBase";
import { TimeBasedRequest } from "./Tool/dataUpdater";

/**
 * Sdk 管理
 */
export default class SdkManager {
    private static _instance = null;
    private _sdkmanager: ISdkManager = null;

    public static Instance(): SdkManager {
        if (!SdkManager._instance) {
            SdkManager._instance = new SdkManager();
        }

        return SdkManager._instance;
    }

    /**初始化 */
    public init() {
        if (cc.sys.isBrowser || this._sdkmanager) return;
        if (cc.sys.platform == cc.sys.WECHAT_GAME) {
            console.log("微信小游戏");
            this._sdkmanager = new WxSdkManager();
        } else if (cc.sys.platform == cc.sys.BYTEDANCE_GAME) {
            this._sdkmanager = new ByteSdkManager();
            console.log("抖音小游戏");
        }

        this._sdkmanager.init();
    }

    // 返回status 1.上架审核中，2.审核通过，3.关闭客户小程序 // 晚上 4:00 到 5:00
    public async isErroAction() {
        let gameStatus = cc.sys.localStorage.getItem("gameStatus");
        if (gameStatus && gameStatus == 3) {
            cc.director.loadScene("ErroScene");
            return false;
        } else {
            let isCanGame: boolean = true;
            await TimeBasedRequest.checkAndRequestData(4, 0, 5, 0).then((status: number | boolean): void => {
                console.log('返回的状态:' + status);
                cc.sys.localStorage.setItem("gameStatus", status + "");
                if (3 == status) {
                    cc.director.loadScene("ErroScene");
                    isCanGame = false;
                }
            });

            return isCanGame;
        }
    }
    /**分享 */
    public share(cb: Function) {
        if (cc.sys.isBrowser) return;
        this._sdkmanager.share(cb);
    }

    /**显示插屏广告*/
    public showInterstitialAd() {
        if (cc.sys.isBrowser) return;
        this._sdkmanager.showInterstitialAd();
    }

    /**播放激励视屏广告 */
    public showVideo(call: Function) {
        if (cc.sys.isBrowser) {
            call(1);
            return;
        }
        this._sdkmanager.showVideo(call);
    }

    /**领取侧边栏奖励 */
    public getSideBarReward() {
        localStorage.setItem("isGetFromeSidebar", "1");
    }

    /**
     * 是否显示侧边栏进入奖励入口
     */
    public isShowSidebar(): boolean {
        if (cc.sys.platform != cc.sys.BYTEDANCE_GAME) {
            return false;
        }

        if (!window["supportSidebar"]) {
            return false;
        }

        if (localStorage.getItem("isGetFromeSidebar")) {
            return false;
        }

        return true;
    }
    
    /**
     * 显示侧边栏进入奖励弹框
     * @param node： 礼包按钮节点
     * @param cb 领取奖励按钮回调
     */
    public showSidebarRewardDialog(cb: Function) {
        ResLoadHelper.load("SidebarRewardDialog", cc.Prefab, (err: Error, res: cc.Prefab) => {
            if (err) {
                console.log('加载错误', "SidebarRewardDialog", err)
                return
            }

            let win: cc.Node = cc.instantiate(res);
            win.addComponent("SidebarRewardDialog");
            win.getComponent(SdkUIBase).initData({cb: cb}, "SidebarRewardDialog");
            cc.find("Canvas").addChild(win, 999);
        }, "ZsSdkRes");
    }
}
